<?php


namespace app\common\model;


use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use app\common\model\OrderAftersale;

class OrderProof2 extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'play_order_proof';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = false;
    protected $connection = 'slavedatabase';

    // 追加属性
    protected $append = [
        'one_image_url',
        'two_image_url',
        'three_image_url',
        'upload_time_text',
        'machine_audit_state_text'
    ];

    public function user()
    {
        return $this->belongsTo('app\common\model\User2');
    }

    public function getOneImageUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['one_image']) ? $data['one_image'] : '');
        return config('upload_domain').$value;
    }

    public function getTwoImageUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['two_image']) ? $data['two_image'] : '');
        return config('upload_domain').$value;
    }

    public function getThreeImageUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['three_image']) ? $data['three_image'] : '');
        return config('upload_domain').$value;
    }

    public function getUploadTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['upload_time']) ? $data['upload_time'] : '');
        return is_numeric($value) ? date("Y/m/d H:i", $value) : $value;
    }

    public function getMachineAuditDescAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['machine_audit_desc']) ? $data['machine_audit_desc'] : '');
        if ($value) {
            $arr = json_decode($value,1);
            $value = '';
            if ($arr && count($arr)) {
                foreach ($arr as $k => $v) {
                    if ($v==1) {
                        $value .= $k.'（合格），';
                    } else {
                        $value .= $k.'（不合格），';
                    }
                }
            }
            $value = trim($value,'，');
        }
        return $value;
    }

    public function getMachineAuditStateList()
    {
        return ['1' => '通过', '-1'=>'不通过'];
    }

    public function getMachineAuditStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['machine_audit_state']) ? $data['machine_audit_state'] : '');
        $list = $this->getMachineAuditStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function reject(int $id = 0, array $extend = [], $nickname, $user_id)
    {
        Db::startTrans();
        try {
            $orderProof = $this->where([
                'id' => $id,
                'handle_status' => 1
            ])->find();
            $order = (new Order2())->where(['play_order_no' => $orderProof['play_order_no'], 'order_status' => 3])->find();
            if (isset($extend['handle_admin_id']) && $extend['handle_admin_id'] > 0)
                $orderProof->handle_admin_id = $extend['handle_admin_id'];
            if (isset($extend['handle_merchant_id']) && $extend['handle_merchant_id'] > 0)
                $orderProof->handle_merchant_id = $extend['handle_merchant_id'];
            if (isset($extend['handle_admin_desc']) && $extend['handle_admin_desc'] != '')
                $orderProof->handle_admin_desc = $extend['handle_admin_desc'];
            if (isset($extend['handle_merchant_desc']) && $extend['handle_merchant_desc'] != '')
                $orderProof->handle_merchant_desc = $extend['handle_merchant_desc'];
            // 产生售后事件
            $OrderAftersale = new OrderAftersale2();
            $OrderAftersale->save([
                'submit_user' => $nickname,
                'submit_user_id' => $user_id,
                'type' => '商家提交异常',
                'play_order_no' => $orderProof['play_order_no'],
                'play_no' => $order['play_no'],
                'content' => $orderProof->handle_merchant_desc,
            ]);
            $orderProof->handle_status = 0;
            $orderProof->handle_time = time();
            $orderProof->order_aftersale_id = $OrderAftersale->id;
            $orderProof->save();
            $order->order_status = -7;
            $order->save();
            // orderlog
            OrderLog2::create([
                'play_order_id' => $order['id'],
                'play_order_no' => $order['play_order_no'],
                'create_by' => $nickname,
                'create_by_id' => $orderProof['handle_merchant_id'],
                'is_front' => 1,
                'info' => '商家上传异常',
            ]);
            Db::commit();
            return true;
        } catch (Exception $exception) {
            Db::rollback();
            return false;
        }
    }
}